﻿// Decompiled with JetBrains decompiler
// Type: pvdcd.Project
// Assembly: pvdcd, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 8BE7F426-72A0-448B-ADB3-CA2AC8E1D830
// Assembly location: C:\Users\KasimGulcan\Desktop\Kosgeb\DynaCAD\Project\DynaCAD\bin\Debug\pvdcd.dll

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using XO1;

namespace pvdcd
{
  [Serializable]
  public class Project
  {
    public string ProjectName;
    public string PreparedBy;
    public string Customer_Representative;
    public string Plan;
    public string Technical_Drawing;
    public List<PartWithProperty> PartList;
    public List<TableProperty> CollarTableProperties;
    [NonSerialized]
    public Table CollarTable;
    public string CollarTableTitle;
    public List<TableProperty> PocketTableProperties;
    [NonSerialized]
    public Table PocketTable;
    public string PocketTableTitle;
    public List<TableProperty> CollarLableTableProperties;
    [NonSerialized]
    public Table CollarLableTable;
    public string CollarLableTableTitle;
    public List<TableProperty> ApaturaTableProperties;
    [NonSerialized]
    public Table ApaturaTable;
    public string ApaturaTableTitle;
    [NonSerialized]
    public Canvas ShapeGeneralView;
    [NonSerialized]
    public Canvas ShapeCollar;
    [NonSerialized]
    public Canvas ShapeFrontSize;
    [NonSerialized]
    public Canvas ShapeBackSize1;
    [NonSerialized]
    public Canvas ShapeBackSize2;
    [NonSerialized]
    public Canvas ShapeIron;

    public Project()
    {
      this.PartList = new List<PartWithProperty>();
      this.CollarTableProperties = new List<TableProperty>();
      this.CollarTableProperties.Add(new TableProperty());
      this.CollarTableProperties.Add(new TableProperty());
      this.CollarTableProperties.Add(new TableProperty());
      this.CollarTableProperties[0].PropertyName = "Yaka Ucu";
      this.CollarTableProperties[1].PropertyName = "Yaka Ort.";
      this.CollarTableProperties[2].PropertyName = "Kuşak Ort.";
      this.CollarTableTitle = "Yaka ile İlgili Ölçüler";
      this.CollarTable = Enumerable.First<Table>(Enumerable.OfType<Table>((IEnumerable) DetailView.GetTable(this.CollarTableProperties, this.CollarTableTitle).Blocks));
      this.PocketTableProperties = new List<TableProperty>();
      this.PocketTableProperties.Add(new TableProperty());
      this.PocketTableProperties.Add(new TableProperty());
      this.PocketTableProperties.Add(new TableProperty());
      this.PocketTableProperties.Add(new TableProperty());
      this.PocketTableProperties[0].PropertyName = "X";
      this.PocketTableProperties[1].PropertyName = "Y";
      this.PocketTableProperties[2].PropertyName = "CB";
      this.PocketTableProperties[3].PropertyName = "CE";
      this.PocketTableTitle = "Cep Takım Yeri ve Cep Ölçüleri";
      this.PocketTable = Enumerable.First<Table>(Enumerable.OfType<Table>((IEnumerable) DetailView.GetTable(this.PocketTableProperties, this.PocketTableTitle).Blocks));
      this.CollarLableTableProperties = new List<TableProperty>();
      this.CollarLableTableProperties.Add(new TableProperty());
      this.CollarLableTableProperties.Add(new TableProperty());
      this.CollarLableTableProperties.Add(new TableProperty());
      this.CollarLableTableProperties.Add(new TableProperty());
      this.CollarLableTableProperties.Add(new TableProperty());
      this.CollarLableTableProperties.Add(new TableProperty());
      this.CollarLableTableProperties.Add(new TableProperty());
      this.CollarLableTableProperties.Add(new TableProperty());
      this.CollarLableTableProperties[0].PropertyName = "X";
      this.CollarLableTableProperties[1].PropertyName = "Y";
      this.CollarLableTableProperties[2].PropertyName = "Z";
      this.CollarLableTableProperties[3].PropertyName = "K";
      this.CollarLableTableTitle = "Etiket ile İlgili Ölçüler";
      this.CollarLableTable = Enumerable.First<Table>(Enumerable.OfType<Table>((IEnumerable) DetailView.GetTable(this.CollarLableTableProperties, this.CollarLableTableTitle).Blocks));
      this.ApaturaTableProperties = new List<TableProperty>();
      this.ApaturaTableProperties.Add(new TableProperty());
      this.ApaturaTableProperties.Add(new TableProperty());
      this.ApaturaTableProperties.Add(new TableProperty());
      this.ApaturaTableProperties.Add(new TableProperty());
      this.ApaturaTableProperties.Add(new TableProperty());
      this.ApaturaTableProperties.Add(new TableProperty());
      this.ApaturaTableProperties.Add(new TableProperty());
      this.ApaturaTableProperties.Add(new TableProperty());
      this.ApaturaTableProperties[0].PropertyName = "A";
      this.ApaturaTableProperties[1].PropertyName = "B";
      this.ApaturaTableProperties[2].PropertyName = "C";
      this.ApaturaTableProperties[3].PropertyName = "AB";
      this.ApaturaTableProperties[4].PropertyName = "XC";
      this.ApaturaTableProperties[5].PropertyName = "XX";
      this.ApaturaTableTitle = "Apatura+Bile+Pile ile İlgili Ölçüler";
      this.ApaturaTable = Enumerable.First<Table>(Enumerable.OfType<Table>((IEnumerable) DetailView.GetTable(this.ApaturaTableProperties, this.ApaturaTableTitle).Blocks));
    }

    public void Save()
    {
      this.UpdateProject();
      string path = Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + ".prj";
      if (!Directory.Exists(Directory.GetCurrentDirectory() + "\\Projects\\"))
        Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Projects\\");
      FileStream fileStream1 = new FileStream(path, FileMode.Create);
      new BinaryFormatter().Serialize((Stream) fileStream1, (object) this);
      fileStream1.Close();
      File.WriteAllText(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_GeneralView.xaml", string.Empty);
      FileStream fileStream2 = new FileStream(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_GeneralView.xaml", FileMode.OpenOrCreate);
      XamlWriter.Save((object) this.ShapeGeneralView, (Stream) fileStream2);
      fileStream2.Close();
      File.WriteAllText(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_Collar.xaml", string.Empty);
      FileStream fileStream3 = new FileStream(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_Collar.xaml", FileMode.OpenOrCreate);
      XamlWriter.Save((object) this.ShapeCollar, (Stream) fileStream3);
      fileStream3.Close();
      File.WriteAllText(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_FrontSize.xaml", string.Empty);
      FileStream fileStream4 = new FileStream(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_FrontSize.xaml", FileMode.OpenOrCreate);
      XamlWriter.Save((object) this.ShapeFrontSize, (Stream) fileStream4);
      fileStream4.Close();
      File.WriteAllText(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_BackSize1.xaml", string.Empty);
      FileStream fileStream5 = new FileStream(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_BackSize1.xaml", FileMode.OpenOrCreate);
      XamlWriter.Save((object) this.ShapeBackSize1, (Stream) fileStream5);
      fileStream5.Close();
      File.WriteAllText(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_BackSize2.xaml", string.Empty);
      FileStream fileStream6 = new FileStream(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_BackSize2.xaml", FileMode.OpenOrCreate);
      XamlWriter.Save((object) this.ShapeBackSize2, (Stream) fileStream6);
      fileStream6.Close();
      File.WriteAllText(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_Iron.xaml", string.Empty);
      FileStream fileStream7 = new FileStream(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "_Iron.xaml", FileMode.OpenOrCreate);
      XamlWriter.Save((object) this.ShapeIron, (Stream) fileStream7);
      fileStream7.Close();
      foreach (PartWithProperty partWithProperty in this.PartList)
      {
        partWithProperty.Part.PartUpDate();
        
          partWithProperty.Part.aSave(Directory.GetCurrentDirectory() + "\\Projects\\" + this.ProjectName + "Parts");
          //TODO: aSave
      }
    }

    public Project Load(string FileName)
    {
      FileStream fileStream = new FileStream(FileName, FileMode.Open);
      Project project = (Project) new BinaryFormatter().Deserialize((Stream) fileStream);
      fileStream.Close();
      Canvas canvas1 = XamlFileOperations.OpenXaml(FileName.Replace(".prj", "_GeneralView.xaml"));
      Canvas canvas2 = XamlFileOperations.OpenXaml(FileName.Replace(".prj", "_Collar.xaml"));
      Canvas canvas3 = XamlFileOperations.OpenXaml(FileName.Replace(".prj", "_FrontSize.xaml"));
      Canvas canvas4 = XamlFileOperations.OpenXaml(FileName.Replace(".prj", "_BackSize1.xaml"));
      Canvas canvas5 = XamlFileOperations.OpenXaml(FileName.Replace(".prj", "_BackSize2.xaml"));
      Canvas canvas6 = XamlFileOperations.OpenXaml(FileName.Replace(".prj", "_Iron.xaml"));
      project.ShapeGeneralView = canvas1;
      project.ShapeCollar = canvas2;
      project.ShapeFrontSize = canvas3;
      project.ShapeBackSize1 = canvas4;
      project.ShapeBackSize2 = canvas5;
      project.ShapeIron = canvas6;

        //TODO:
      //foreach (PartWithProperty partWithProperty in project.PartList)
      //  partWithProperty.Part = Part.LoadPart(Directory.GetCurrentDirectory() + "\\Projects\\" + project.ProjectName + "Parts\\" + partWithProperty.Part.TemplateName + "\\" + partWithProperty.Part.PartType.PartType + "\\" + partWithProperty.Part.PartName + ".obj");
      
      project.DriveProject();
      return project;
    }

    public void UpdateProject()
    {
      this.CollarTableProperties = DetailView.GetTableProperties(this.CollarTable);
      this.CollarTableTitle = ((TextBox) ((BlockUIContainer) this.CollarTable.RowGroups[0].Rows[0].Cells[0].Blocks.LastBlock).Child).Text;
      this.PocketTableProperties = DetailView.GetTableProperties(this.PocketTable);
      this.PocketTableTitle = ((TextBox) ((BlockUIContainer) this.PocketTable.RowGroups[0].Rows[0].Cells[0].Blocks.LastBlock).Child).Text;
      this.CollarLableTableProperties = DetailView.GetTableProperties(this.CollarLableTable);
      this.CollarLableTableTitle = ((TextBox) ((BlockUIContainer) this.CollarLableTable.RowGroups[0].Rows[0].Cells[0].Blocks.LastBlock).Child).Text;
      this.ApaturaTableProperties = DetailView.GetTableProperties(this.ApaturaTable);
      this.ApaturaTableTitle = ((TextBox) ((BlockUIContainer) this.ApaturaTable.RowGroups[0].Rows[0].Cells[0].Blocks.LastBlock).Child).Text;
    }

    public void DriveProject()
    {
      this.CollarTable = Enumerable.First<Table>(Enumerable.OfType<Table>((IEnumerable) DetailView.GetTable(this.CollarTableProperties, this.CollarTableTitle).Blocks));
      this.PocketTable = Enumerable.First<Table>(Enumerable.OfType<Table>((IEnumerable) DetailView.GetTable(this.PocketTableProperties, this.PocketTableTitle).Blocks));
      this.CollarLableTable = Enumerable.First<Table>(Enumerable.OfType<Table>((IEnumerable) DetailView.GetTable(this.CollarLableTableProperties, this.CollarLableTableTitle).Blocks));
      this.ApaturaTable = Enumerable.First<Table>(Enumerable.OfType<Table>((IEnumerable) DetailView.GetTable(this.ApaturaTableProperties, this.ApaturaTableTitle).Blocks));
    }

    public static void OpenHelp()
    {
      string currentDirectory = Directory.GetCurrentDirectory();
      try
      {
        Process.Start(currentDirectory + "\\DynaCadHelp.chm\\");
      }
      catch
      {
      }
    }
  }
}
